﻿<%@ Page Language="C#" %>

<!DOCTYPE html>
<script runat="server">

    static CommonLib.DbHelper.IDBhelper dbHelper = new CommonLib.DbHelper.Factory().IDBhelper;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            var sdr1 = dbHelper.ExecuteReader("select id,REPLICATE('　', Grade - 1) + Name AS Name from System_City where Grade>0 and IsDelete =0 order by Levels");
            DistrictDropDownList.DataSource = sdr1;
            DistrictDropDownList.DataTextField = "Name";
            DistrictDropDownList.DataValueField = "id";
            DistrictDropDownList.DataBind();
            DistrictDropDownList.Items.Insert(0, new ListItem("其它", "0"));
            sdr1.Close();

            DataBind_data();
        }
    }

    private void DataBind_data()
    {

        var sdr = dbHelper.ExecuteReader("SELECT * FROM " + Request["tn"].ToString() + " WHERE id = '" + Request["id"].ToString() + "'");
        if (sdr.Read())
        {
            标识Literal.Text = sdr["id"].ToString();
            父标识Literal.Text = sdr["pid"].ToString();
            级别Literal.Text = sdr["Grade"].ToString();
            叶子Literal.Text = sdr["Leaf"].ToString();

            层次Literal.Text = sdr["Levels"].ToString();
            名称TextBox.Text = sdr["Name"].ToString();

            var sql = "";
            if (sql != "")
            {
                var sdr1 = dbHelper.ExecuteReader(sql);
                DistrictIDDropDownList.DataSource = sdr1;
                DistrictIDDropDownList.DataValueField = "id";
                DistrictIDDropDownList.DataTextField = "Name";
                DistrictIDDropDownList.DataBind();
                sdr1.Close();
                DistrictIDDropDownList.Items.Insert(0, new ListItem() { Text = "不限", Value = "" });
                DistrictIDDropDownList.SelectedIndex = -1;
                try
                {
                    DistrictIDDropDownList.Items.FindByValue(sdr["DistrictID"].ToString().ToLower()).Selected = true;
                }
                catch { }
            }
            else
            {
                DistrictIDDropDownList.Items.Insert(0, new ListItem() { Text = "不限", Value = "" });
                DistrictIDDropDownList.SelectedIndex = -1;
                District.Visible = false;
            }

            setDropDownList(禁用DropDownList, sdr["Disable"].ToString(), "否");
            setDropDownList(节点状态DropDownList, sdr["State"].ToString(), "open");

            if (sdr["Leaf"].ToString().ToLower() == "true")
            {
                节点状态DropDownList.Enabled = false;
            }
        }
        sdr.Close();
    }

    private void setDropDownList(DropDownList list, string value, string defaultValue)
    {
        try
        {
            list.Items.FindByValue(value).Selected = true;
        }
        catch
        {
            list.Items.FindByValue(defaultValue).Selected = true;
        }
    }

    protected void 重置Button_Click(object sender, EventArgs e)
    {
        DataBind_data();
    }

    protected void 提交Button_Click(object sender, EventArgs e)
    {
        var type = 机构类型DropDownList.Text;
        SqlParameter[] P = {
                                new SqlParameter("@id", Request["id"].ToString()),
                                new SqlParameter("@pid", 父标识Literal.Text),
                                new SqlParameter("@Grade", 级别Literal.Text),
                                new SqlParameter("@Leaf", 叶子Literal.Text),
                                new SqlParameter("@Levels", 层次Literal.Text),
                                new SqlParameter("@Name", 名称TextBox.Text),
                                //new SqlParameter("@Type", type),
                                //new SqlParameter("@DistrictID",DistrictIDDropDownList.Text),
                                new SqlParameter("@Disable", 禁用DropDownList.Text) ,
                                new SqlParameter("@State", 节点状态DropDownList.Text) ,
                                //new SqlParameter("@SystemCategory", SystemDropDownList.Text) 
                            };

        dbHelper.ExecuteNonQuery(@"UPDATE " + Request["tn"].ToString() + @"
                                    SET pid=@pid, Grade=@Grade, Leaf=@Leaf, Levels=@Levels, Name=@Name,  Disable=@Disable,State=@State
                                    WHERE (id = @id)", P);

        //更新父级页面节点名称
        JSLiteral.Text = "<script>";
        JSLiteral.Text += "var node = parent.$(\"#tree\", window.parent.document).tree(\"find\", \"" + Request["id"].ToString() + "\");";
        JSLiteral.Text += "node.text = \"" + 名称TextBox.Text + "\";";
        JSLiteral.Text += "parent.$(\"#tree\", window.parent.document).tree(\"update\", node);";
        JSLiteral.Text += "</" + "script>";

        //dbHelper.ExecuteNonQuery("DELETE FROM ICity_Org where OrgID='" + Request["id"].ToString() + "'");
        //var str = "INSERT INTO ICity_Org (CityID, OrgID) VALUES('" + DistrictDropDownList.Text + "','" + Request["id"].ToString() + "')";
        //dbHelper.ExecuteNonQuery(str);

        DataBind_data();
    }
</script>


<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <meta http-equiv="X-UA-Compatible" content="IE=Edge" />
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>树属性管理</title>
    <style type="text/css">
        .btnCancel {
            margin-left: 6px;
        }

        .input {
            width: 450px;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
        <table cellspacing="10" cellpadding="0" border="0" style="background-color: #F2F7FC; border: #CCE0F5 solid 1px; font-size: 12px; width: 100%;">
            <tr style="background-color: #F2F7FC;">
                <td colspan="2">
                    <table width="100%">
                        <tr>
                            <td width="100px" align="right">标识：</td>
                            <td>
                                <asp:Literal ID="标识Literal" runat="server" /></td>
                        </tr>
                        <tr style="display: none">
                            <td width="100px" align="right">父标识：</td>
                            <td>
                                <asp:Literal ID="父标识Literal" runat="server" /></td>
                        </tr>
                        <tr>
                            <td width="100px" align="right">叶子：</td>
                            <td>
                                <asp:Literal ID="叶子Literal" runat="server" /></td>

                        </tr>
                        <tr>
                            <td width="100px" align="right">级别：</td>
                            <td>
                                <asp:Literal ID="级别Literal" runat="server" /></td>
                        </tr>
                        <tr>
                            <td width="100px" align="right">层次：</td>
                            <td>
                                <asp:Literal ID="层次Literal" runat="server" /></td>
                        </tr>
                        <tr>
                            <td width="100px" align="right">名称：</td>
                            <td>
                                <asp:TextBox ID="名称TextBox" runat="server" CssClass="input" /></td>
                        </tr>
                        <tr>
                            <td width="100px" align="right">系统模块：</td>
                            <td>
                                <asp:DropDownList ID="SystemDropDownList" runat="server" Width="250px">
                                </asp:DropDownList>
                            </td>
                        </tr>
                        <tr>
                            <td width="100px" align="right">对应行政区：</td>
                            <td>
                                <asp:DropDownList ID="DistrictDropDownList" runat="server" Width="250px">
                                </asp:DropDownList>
                            </td>
                        </tr>
                        <tr>
                            <td width="100px" align="right">机构类型：</td>
                            <td>
                                <asp:DropDownList ID="机构类型DropDownList" runat="server">
                                </asp:DropDownList>
                            </td>
                        </tr>
                        <tr runat="server" id="District">
                            <td width="100px" align="right" style="color: red;">
                                <asp:Literal ID="Literal1" runat="server"></asp:Literal>：</td>
                            <td>
                                <asp:DropDownList ID="DistrictIDDropDownList" runat="server"></asp:DropDownList>
                                <%--<asp:TextBox ID="区政府IDTextBox" runat="server" CssClass="input" />--%>
                            </td>
                        </tr>
                        <tr style="display: none;">
                            <td width="100px" align="right">显示类型：</td>
                            <td>
                                <asp:DropDownList ID="显示类型DropDownList" runat="server">
                                    <asp:ListItem Value="" Text="不限"></asp:ListItem>
                                    <asp:ListItem Value="单页" Text="单页"></asp:ListItem>
                                    <asp:ListItem Value="文字列表" Text="文字列表"></asp:ListItem>
                                    <asp:ListItem Value="图文摘要" Text="图文摘要"></asp:ListItem>
                                    <asp:ListItem Value="图片列表" Text="图片列表"></asp:ListItem>
                                </asp:DropDownList>
                            </td>
                        </tr>
                        <tr>
                            <td width="100px" align="right">禁用：</td>
                            <td>
                                <asp:DropDownList ID="禁用DropDownList" runat="server">
                                    <asp:ListItem>是</asp:ListItem>
                                    <asp:ListItem>否</asp:ListItem>
                                </asp:DropDownList>
                            </td>
                        </tr>
                        <tr>
                            <td width="100px" align="right">节点状态：</td>
                            <td>
                                <asp:DropDownList ID="节点状态DropDownList" runat="server">
                                    <asp:ListItem Text="关闭" Value="closed"></asp:ListItem>
                                    <asp:ListItem Text="打开" Value="open"></asp:ListItem>
                                </asp:DropDownList>
                            </td>
                        </tr>
                        <tr>
                            <td width="100px" align="right"></td>
                            <td>
                                <asp:Button ID="重置Button" runat="server" Text="重 置" OnClick="重置Button_Click" />
                                &nbsp;<asp:Button ID="提交Button" runat="server" Text="提 交" OnClick="提交Button_Click" />
                            </td>
                        </tr>
                    </table>
                </td>
            </tr>
        </table>
        <asp:Literal ID="JSLiteral" runat="server" />
    </form>
</body>
</html>
